/**
 * 懒加载
 * @param {String} src 资源路径
 * @param {String} name 全局变量名，对应window[name]
 * @param {Function} callback 加载成功的回调函数
 */
export default (src, name, callback) => {
  if (src.endsWith('css')) {
    const el = document.createElement('link')
    el.rel = 'stylesheet'
    el.href = src
    document.getElementsByTagName('head')[0].appendChild(el)
  } else if (src.endsWith('js')) {
    if (window[name]) {
      callback?.()
    }
    const el = document.createElement('script')
    el.type = 'text/javascript'
    el.async = true
    el.src = src
    el.onload = () => {
      callback?.()
    }
    document.getElementsByTagName('head')[0].appendChild(el)
  }
}